<template>
  <div class="role-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="8">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item >
            <el-button
              icon="el-icon-plus"
              type="primary"
              :disabled="!isSuperAdmin"
              @click="handleEdit"
            >
              添加
            </el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="changePassword">支付密码修改</el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="16">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <!--<el-form-item >
            <el-button type="primary" @click="changePassword">支付密码修改</el-button>
          </el-form-item>-->
          <el-form-item>
            <el-input
              v-model.trim="queryForm.keyword"
              placeholder="请输入角色名称"
              clearable
            />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">
              查询
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      @selection-change="setSelectRows"
    >
      <el-table-column
        align="center"
        show-overflow-tooltip
        label="序号"
        type="index"
        width="95"
      />
      <el-table-column
        align="center"
        show-overflow-tooltip
        prop="role_name"
        label="名称"
      />
      <el-table-column
        align="center"
        show-overflow-tooltip
        prop="create_time"
        label="创建时间"
      />
      <el-table-column align="center" label="操作" width="95">
        <template #default="{ row }">
          <el-button
            type="text"
            :disabled="row.handler == 2"
            @click="handleEdit(row)"
          >
            编辑
          </el-button>
          <el-button
            type="text"
            :disabled="row.handler == 2"
            @click="handleDelete(row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.page"
      :page-size="queryForm.list_row"
      :layout="layout"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <edit ref="edit" @fetch-data="fetchData"/>
    <el-dialog
      title="提示"
      :visible.sync="passwordVisible"
      width="30%">
      <el-form ref="passForm" :model="passwordForm" :rules="passRules" label-width="80px">
        <el-form-item
          label="新密码:"
          prop="pass"
          label-width="120px"
        >
          <el-input
            v-model="passwordForm.pass"
            show-password
            placeholder="请输入新密码"
          />
        </el-form-item>
        <el-form-item
          label="确认密码:"
          prop="two_pass"
          label-width="120px"
        >
          <el-input
            v-model="passwordForm.two_pass"
            show-password
            placeholder="请确认新密码"
          />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="hanldPassOk">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import {getList, doDelete, editpaypass} from '@/api/role'
  import Edit from './components/RoleEdit'
  import {getStaticUserInfo} from '@/utils/user'

  export default {
    name: 'Role',
    components: {Edit},
    data() {
      const password = (rule, value, callback) => {
        if (value && value == this.passwordForm.pass) {
          callback()
        } else {
          return callback(new Error('两次密码不一致'))
        }
      }
      return {
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          page: 1,
          list_row: 10,
          keyword: '',
        },
        isSuperAdmin: !getStaticUserInfo().role_id,
        passwordVisible: false,
        passwordForm: {
          pass: '',
          two_pass: ''
        },
        passRules: {
          pass: [
            {required: true, message: "请输入密码", trigger: "change"},
          ],
          two_pass: [
            {required: true, message: "再次确认密码", trigger: "change"},
            {validator: password, trigger: "change"}
          ]
        }
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      hanldPassOk() {
        this.$refs.passForm.validate(valid => {
          if (valid) {
            editpaypass(this.passwordForm).then(res => {
              this.$notify({
                title: '提示',
                message: res,
                type: 'success'
              });
            })
          }
        })
      },
      changePassword() {
        this.passwordVisible = true
      },
      setSelectRows(val) {
        this.selectRows = val
      },
      handleAssignAuths(row) {
      },
      handleEdit(row) {
        if (row.id) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      handleDelete(row) {
        if (row.id) {
          this.$baseConfirm('你确定要删除当前项吗', null, async () => {
            const msg = await doDelete({role_id: row.id})
            this.$baseMessage(msg, 'success')
            this.fetchData()
          })
        } else {
          if (this.selectRows.length > 0) {
            const ids = this.selectRows.map((item) => item.id).join()
            this.$baseConfirm('你确定要删除选中项吗', null, async () => {
              const {msg} = await doDelete({ids})
              this.$baseMessage(msg, 'success')
              this.fetchData()
            })
          } else {
            this.$baseMessage('未选中任何行', 'error')
            return false
          }
        }
      },
      handleSizeChange(val) {
        this.queryForm.list_row = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.page = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.page = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const {data, total} = await getList(this.queryForm)
        this.list = data
        this.total = total
        this.listLoading = false
      },
    },
  }
</script>
